package com.qf.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.qf.feign.OrderFeignApi;
import com.qf.pojo.R;
import com.qf.service.UserService;
import com.sun.deploy.security.BlockedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//@RestController
//@RequestMapping("/user")
//
//@RefreshScope //当配置发生变化后，该注解标注的对象销毁重建
public class UserController3 {

    @Autowired
    private OrderFeignApi orderFeignApi;

    @Value("${desc}")
    private String desc;

    @GetMapping("/{id}")

    // 假设这个资源是核心资源
    //标识该资源被sentinel保护
    @SentinelResource(value = "findOrders",
//            blockHandler = "findOrders_bh" , //指定的降级方法，只针对流控或者熔断导致的降级。对于其他异常导致的降级不做处理,最后一个参数必须且只能是BlockException  -- 优先级最高
//            fallback = "findOrders_fb" //针对所有类型的异常做降级处理，降级方法的返回值和参数列表要和目标方法保持一致（可选添加异常对象） -- 优先级其次
            defaultFallback = "findOrders_dfb" //针对所有类型的异常做降级处理,降级方法的返回值和目标方法保持一致（可选添加异常对象） -- 优先级最低
    )
    public R findOrders(@PathVariable("id") Integer id) throws InterruptedException {
        System.out.println("--------------" + desc);
        R r = orderFeignApi.findByUid(id);
        return r;
    }

    public R findOrders_bh(Integer id, BlockException e){
        return R.error().putMsg("block降级--" + e.getMessage());
    };

    public R findOrders_fb(Integer id, Throwable e){
        return R.error().putMsg("fallback降级--" + e.getMessage());
    };

    public R findOrders_dfb(Throwable e){
        if (e instanceof FlowException){
            System.out.println("--FlowException"  );
        }else if (e instanceof DegradeException){
            System.out.println("--DegradeException"  );
        }
        return R.error().putMsg("default fallback降级--" + e.getMessage());
    };


    //这个资源是非核心资源
    @GetMapping("/test1")
    //标识该资源被sentinel保护
    @SentinelResource(value = "test1")
    public R test1() throws InterruptedException {
        System.out.println("test1--------------" + desc);

        return R.success().putMsg("test1 success...");
    }


    @Autowired
    private UserService userService;

    /*
        非核心业务，当核心业务访问量过大时，对非核心业务进行流控
     */
    @GetMapping("/test2")
    @SentinelResource("test2")
    public R test2(){
        userService.queryOrder();

        return R.success().putMsg("test2 success...");
    }

    /*
        核心业务
     */
    @GetMapping("/test3")
    @SentinelResource("test3")
    public R test3(){
        userService.queryOrder();

        return R.success().putMsg("test3 success...");
    }
}
